﻿using System;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace ThirdHand
{
    [Serializable]
    public class ThirdHandCommandBindingList : BindingList<ThirdHandCommand>
    {
        private const string CommandFilename = "vscommands.xml";

        internal static ThirdHandCommandBindingList LoadFromStorage()
        {
            ThirdHandCommandBindingList rslt = new ThirdHandCommandBindingList();
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForAssembly();
            XmlSerializer xs = new XmlSerializer(typeof(ThirdHandCommandBindingList));

            try
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(CommandFilename, FileMode.Open, isoStore))
                {
                    rslt = xs.Deserialize(isfs) as ThirdHandCommandBindingList;
                    isfs.Close();
                }
            }
            catch (FileNotFoundException)
            {
                return Revert();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString(),ex.GetType().FullName,System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error,System.Windows.Forms.MessageBoxDefaultButton.Button1,0);
            }
            return rslt;
        }

        internal static ThirdHandCommandBindingList LoadFromFile(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                XmlSerializer xs = new XmlSerializer(typeof(ThirdHandCommandBindingList));
                ThirdHandCommandBindingList rslt = xs.Deserialize(fs) as ThirdHandCommandBindingList;
                fs.Close();

                return rslt;
            }
        }

        internal static ThirdHandCommandBindingList Revert()
        {
            using (StringReader sr = new StringReader(Properties.Resources.InitialSetOfCommands))
            {
                XmlSerializer xs = new XmlSerializer(typeof(ThirdHandCommandBindingList));
                return xs.Deserialize(sr) as ThirdHandCommandBindingList;
            }
        }
        internal void SaveToStorage()
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForAssembly())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(CommandFilename, FileMode.Create, isoStore))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(ThirdHandCommandBindingList));
                    xs.Serialize(isfs, this);
                    isfs.Flush();
                    isfs.Close();
                }
            }
        }

        internal void SaveToFile(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                XmlSerializer xs = new XmlSerializer(typeof(ThirdHandCommandBindingList));
                xs.Serialize(fs, this);
                fs.Flush();
                fs.Close();
            }
        }
    }
}
